<?php
	$error = NULL;
	// Local para salvar a Imagem.
	$destination_path = '..'.DIRECTORY_SEPARATOR.'bancoImgs'.DIRECTORY_SEPARATOR;
	if( $_FILES['foto']['type'] == 'image/jpeg' || $_FILES['foto']['type'] == 'image/jpg' || $_FILES['foto']['type'] == 'image/pjpeg' ){
		// Gera nome da foto.
		require('funcoes.inc.php');
		$link = conecta();
		do{
			$name = md5($_FILES['foto']['name'].time().rand());
			$num = mysql_num_rows(mysql_query("SELECT valor FROM imagem WHERE valor='".$name."'"));
		}while( $num != 0 );
		mysql_query("INSERT INTO imagem ( nome , valor ) VALUES ('".$_FILES['foto']['name']."','".$name.".jpg')",$link);
		
		mysql_close($link);
	
		move_uploaded_file($_FILES['foto']['tmp_name'], $destination_path.$name.'.jpg');

	}
	else
		$error = "alert('Formato Incorreto.');";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<script type="text/javascript"><?php print($error) ?>

window.top.document.stopUpload('<?php echo empty($error)?$name:0; ?>');
//alert(window.top.document.stopUpload );

</script>   

</body>
</html>

